<?php
namespace cosocket\application;

class ErrorCode
{
    // 成功
    const SUCCESS = 0;

    // 客户端错误 (400-499)
    const PARAM_ERROR = 400;
    const UNAUTHORIZED = 401;
    const FORBIDDEN = 403;
    const NOT_FOUND = 404;
    const TOO_MANY_REQUESTS = 429;

    // 服务器错误 (500-599)
    const SERVER_ERROR = 500;
    const NOT_IMPLEMENTED = 501;
    const BAD_GATEWAY = 502;
    const SERVICE_UNAVAILABLE = 503;
    const GATEWAY_TIMEOUT = 504;

    // 业务错误 (1000-9999)
    const USER_NOT_LOGIN = 1001;
    const LOGIN_ERROR = 1002;
    const ACCOUNT_DISABLED = 1003;
    const INSUFFICIENT_BALANCE = 1004;
    const OPERATION_TOO_FREQUENT = 1005;

    // 系统错误 (10000+)
    const SYSTEM_MAINTENANCE = 10000;
    const DB_CONNECTION_ERROR = 10001;
    const CACHE_SERVICE_ERROR = 10002;

    private static $messages = [
        self::SUCCESS => '成功',

        self::PARAM_ERROR => '参数错误',
        self::UNAUTHORIZED => '未授权',
        self::FORBIDDEN => '禁止访问',
        self::NOT_FOUND => '未找到',
        self::TOO_MANY_REQUESTS => '请求过于频繁',

        self::SERVER_ERROR => '服务器内部错误',
        self::NOT_IMPLEMENTED => '未实现',
        self::BAD_GATEWAY => '网关错误',
        self::SERVICE_UNAVAILABLE => '服务不可用',
        self::GATEWAY_TIMEOUT => '网关超时',

        self::USER_NOT_LOGIN => '用户未登录',
        self::LOGIN_ERROR => '账号或密码错误',
        self::ACCOUNT_DISABLED => '账号已被禁用',
        self::INSUFFICIENT_BALANCE => '余额不足',
        self::OPERATION_TOO_FREQUENT => '操作过于频繁',

        self::SYSTEM_MAINTENANCE => '系统维护中',
        self::DB_CONNECTION_ERROR => '数据库连接失败',
        self::CACHE_SERVICE_ERROR => '缓存服务不可用',
    ];

    public static function getMessage(int $code): string
    {
        return self::$messages[$code] ?? '未知错误';
    }
}